<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false" center>
      <template #title>
        <div class="title-text">我的频道</div>
      </template>
      <template #default>
        <van-button
          class="edit-btn"
          type="danger"
          round
          size="mini"
          plain
          @click="isEdit = !isEdit"
        >
          {{ isEdit ? '完成' : '编辑' }}
        </van-button>
      </template>
    </van-cell>
    <van-grid :column-num="4" :gutter="10" class="my-grid">
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="channel.id"
        class="grid-item"
        @click="onMyChannelClick(channel, index)"
      >
        <template #icon>
          <van-icon
            name="clear"
            v-show="isEdit && !fixedChannel.includes(channel.id)"
          ></van-icon>
        </template>
        <template #text>
          <span class="text" :class="{ active: index == value.index }">
            {{ channel.name }}
          </span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- /我的频道 -->
    <!-- 频道推荐 -->
    <van-cell :border="false">
      <template #title>
        <div class="title-text">频道推荐</div>
      </template>
    </van-cell>
    <van-grid :column-num="4" :gutter="10" class="recommend-grid">
      <van-grid-item
        icon="plus"
        v-for="channel in recommendChannels"
        :key="channel.id"
        :text="channel.name"
        class="grid-item"
        @click="onAddChannel(channel)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  data() {
    return {
      AllChannellist: [], // 所有频道列表
      isEdit: false, // 控制编辑状态
      fixedChannel: [0] // 固定频道不允许删除 定义为数组(容易维护)
    }
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    value: {
      type: Object,
      required: true
    }
  },
  computed: {
    // 筛选推荐频道
    // #region
    // recommendChannels() {
    //   const channels = []
    //   this.AllChannellist.filter((channel) => {
    //     const ret = this.myChannel.find((myChannel) => {
    //       return channel.id === myChannel.id
    //     })
    //     if (!ret) {
    //       channels.push(channel)
    //     }
    //   })
    //   return channels
    // }
    // #endregion
    recommendChannels() {
      return this.AllChannellist.filter((channel) => {
        return !this.myChannels.find((myChannel) => {
          return channel.id === myChannel.id
        })
      })
    },
    // vuex的user登录信息
    ...mapState(['user'])
  },
  created() {
    // 加载所有频道
    this.loadAllChannels()
  },
  methods: {
    // 加载所有频道的方法
    async loadAllChannels() {
      try {
        const { data: res } = await this.$api.getAllChannels()
        console.log(res)
        this.AllChannellist = res.data.channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    // 用户添加频道
    async onAddChannel(channel) {
      this.myChannels.push(channel)
      // 更新用户频道
      if (this.user) {
        // 已登录
        try {
          await this.$api.addAllChannels({
            id: channel.id, // 频道id
            seq: this.myChannels.length // 序号
          })
        } catch (err) {
          console.log(err)
          this.$toast('保存失败,请稍后重试')
        }
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    // 编辑频道或者切换频道
    onMyChannelClick(channel, index) {
      // #region
      // if (this.isEdit) {
      //   // 编辑状态 删除操作
      //   if (this.fixedChannel.includes(channel.id)) return
      //   if (index <= this.value) {
      //     this.$emit('update-active', index - 1, true)
      //   }
      //   this.myChannels.splice(index, 1)
      // } else {
      //   // 非编辑状态 切换频道操作
      //   this.$emit('update-active', index, false)
      // }
      // #endregion
      if (this.isEdit) {
        if (this.fixedChannel.includes(channel.id)) return
        // 删除频道项
        this.myChannels.splice(index, 1)
        this.deleteChannel(channel)
        if (index === this.value.index) {
          this.$emit('input', { index: index - 1, isChannelEditShow: true })
        }
      } else {
        this.$emit('input', { index, isChannelEditShow: false })
      }
    },
    async deleteChannel(channel) {
      if (this.user) {
        // 已登录,将数据更新到线上
        try {
          await this.$api.deleteAllChannels(channel.id)
        } catch (err) {
          this.$toast('操作失败请稍后重试')
        }
      } else {
        // 未登录,将数据更新到本地
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border-color: #f85959;
    margin-bottom: 10px;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      background-color: #f4f5f6;
      white-space: nowrap;
    }
    .van-grid-item__text,
    .text {
      font-size: 28px;
      color: #222;
      margin-top: 0;
    }
    .text.active {
      color: #f85959;
    }
  }
  /deep/ .recommend-grid {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        top: -10px;
        right: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
      .van-grid-item__icon-wrapper {
        position: static;
      }
    }
  }
}
</style>
